from PyQt5.QtWidgets import (QHBoxLayout, QLabel, QPushButton, QDoubleSpinBox,
                             QLineEdit, QFileDialog, QCheckBox, QButtonGroup,
                             QRadioButton, QVBoxLayout)
from components.actiondialog import ActionDialog


class ImageLocateDialog(ActionDialog):
    def __init__(self, action=None, parent=None):
        super().__init__(parent)

        self.action = action if action else {
            "type": "image_locate",
            "image_path": "",
            "confidence": 0.9,
            "click": True,
            "click_type": "double",  # default to double-click
            "desc": ""
        }

        # 图片路径
        path_layout = QHBoxLayout()
        path_layout.addWidget(QLabel("图片路径:"))
        self.path_edit = QLineEdit()
        self.path_edit.setText(self.action.get("image_path", ""))
        path_layout.addWidget(self.path_edit)

        browse_button = QPushButton("浏览...")
        browse_button.clicked.connect(self.browse_image)
        path_layout.addWidget(browse_button)

        # 置信度
        confidence_layout = QHBoxLayout()
        confidence_layout.addWidget(QLabel("置信度阈值:"))
        self.confidence_spin = QDoubleSpinBox()
        self.confidence_spin.setRange(0.1, 1.0)
        self.confidence_spin.setSingleStep(0.05)
        self.confidence_spin.setValue(self.action.get("confidence", 0.9))
        confidence_layout.addWidget(self.confidence_spin)

        # 是否点击
        click_layout = QVBoxLayout()
        self.click_check = QCheckBox("找到时点击")
        self.click_check.setChecked(self.action.get("click", True))
        click_layout.addWidget(self.click_check)

        # 点击类型 (单机/双击)
        self.click_type_group = QButtonGroup()
        click_type_layout = QHBoxLayout()
        click_type_layout.addWidget(QLabel("点击类型:"))

        self.single_click_radio = QRadioButton("单击")
        self.double_click_radio = QRadioButton("双击")

        self.click_type_group.addButton(self.single_click_radio)
        self.click_type_group.addButton(self.double_click_radio)

        click_type_layout.addWidget(self.single_click_radio)
        click_type_layout.addWidget(self.double_click_radio)

        # Set default selection
        click_type = self.action.get("click_type", "double")
        if click_type == "single":
            self.single_click_radio.setChecked(True)
        else:
            self.double_click_radio.setChecked(True)

        click_layout.addLayout(click_type_layout)

        # 按钮
        button = QPushButton("确定")
        button.clicked.connect(self.accept)

        self.layout.addLayout(path_layout)
        self.layout.addLayout(confidence_layout)
        self.layout.addLayout(click_layout)
        self.layout.addWidget(button)

    def browse_image(self):
        filename, _ = QFileDialog.getOpenFileName(
            self, "选择图片文件", "", "图片文件 (*.png *.jpg *.jpeg *.bmp);;所有文件 (*)")
        if filename:
            self.path_edit.setText(filename)

    def accept(self):
        image_path = self.path_edit.text()
        confidence = self.confidence_spin.value()
        click = self.click_check.isChecked()
        click_type = "single" if self.single_click_radio.isChecked() else "double"

        self.action = {
            "type": "image_locate",
            "image_path": image_path,
            "confidence": confidence,
            "click": click,
            "click_type": click_type,
            "desc": f"定位图片 '{image_path}' (置信度: {confidence})" +
                    (f" 并{click_type}点击" if click else "")
        }
        super().accept()